package com.ny.zmb.openapi.util;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.WriterException;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Base64;
import java.util.Hashtable;

public class QRCode {
    public static void generateQRCode(String url, String filePath) {
        try {
            // 设置二维码的编码规则
            Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
            hints.put(EncodeHintType.MARGIN, 2);  // 设置二维码边距

            // 创建二维码生成器
            QRCodeWriter qrCodeWriter = new QRCodeWriter();

            // 生成二维码的矩阵
            BitMatrix bitMatrix = qrCodeWriter.encode(url, BarcodeFormat.QR_CODE, 300, 300, hints);

            // 创建BufferedImage用于保存二维码图像
            int width = bitMatrix.getWidth();
            int height = bitMatrix.getHeight();
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

            // 将bitMatrix的内容绘制到BufferedImage中
            for (int x = 0; x < width; x++) {
                for (int y = 0; y < height; y++) {
                    image.setRGB(x, y, bitMatrix.get(x, y) ? 0x000000 : 0xFFFFFF);
                }
            }

            // 保存二维码图片到文件
            File file = new File(filePath);
            ImageIO.write(image, "PNG", file);
            System.out.println("二维码生成成功，保存路径：" + filePath);

        } catch (WriterException | IOException e) {
            e.printStackTrace();
        }
    }

    // 生成二维码并返回Base64编码
    public static String generateQRCodeBase64(String url) {
        try {
            // 设置二维码的编码规则
            Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
            hints.put(EncodeHintType.MARGIN, 2);  // 设置二维码边距

            // 创建二维码生成器
            QRCodeWriter qrCodeWriter = new QRCodeWriter();

            // 生成二维码的矩阵
            BitMatrix bitMatrix = qrCodeWriter.encode(url, BarcodeFormat.QR_CODE, 300, 300, hints);

            // 创建BufferedImage用于保存二维码图像
            int width = bitMatrix.getWidth();
            int height = bitMatrix.getHeight();
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

            // 将bitMatrix的内容绘制到BufferedImage中
            for (int x = 0; x < width; x++) {
                for (int y = 0; y < height; y++) {
                    image.setRGB(x, y, bitMatrix.get(x, y) ? 0x000000 : 0xFFFFFF);
                }
            }

            // 将BufferedImage转换为ByteArrayOutputStream
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ImageIO.write(image, "PNG", byteArrayOutputStream);
            byteArrayOutputStream.flush();

            // 转换为Base64编码
            byte[] imageBytes = byteArrayOutputStream.toByteArray();
            String base64EncodedImage = Base64.getEncoder().encodeToString(imageBytes);
            byteArrayOutputStream.close();

            return "data:image/png;base64," + base64EncodedImage;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        String url = "http://localhost:8080" + "/mobile/pages/register/register?invitationCode=" + "abcdef";  // 你希望生成二维码的URL
        String filePath = "QRCode.png";         // 二维码保存的路径
        generateQRCode(url, filePath);
        String base64 = generateQRCodeBase64(url);
        System.out.println("base64" + base64);
    }
}
